package com.ivanceras.client.view;

import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.client.Choice;
import com.ivanceras.client.EditQuestionCaption;
import com.ivanceras.client.Question;

public class QuestionViewer extends Viewer{
	public Question question;
	public Long itemNo;
	Image indicator = new Image();
	public HTML itemNumber = new HTML();
	public QuestionViewer(Question question){
		this.question = question;
	}
	public void setItemNumber(Long number){
		question.itemNo = number;
		itemNo = number;
		itemNumber.setHTML("<b>"+number+"</b>. ");
	}
	public Widget getWidget(){
		VerticalPanel vpanel = new VerticalPanel();
		HorizontalPanel hpanel = new HorizontalPanel();
		hpanel.setWidth("100%");
		//hpanel.add(indicator);
		hpanel.add(new HTML("<b>"+question.text+"</b>"));
		EditQuestionCaption editQn = new EditQuestionCaption("edit",question);
		editQn.question = question;
		hpanel.add(editQn);
		vpanel.add(hpanel);
		HTML vspacer = new HTML();
		vspacer.setHeight("10px");
		vpanel.add(vspacer);
		for(int j = 0; j < question.choiceList.size(); j++){
			Choice choice = question.choiceList.get(j);
			choice.itemNo = question.itemNo;
			ChoiceViewer choiceViewer = new ChoiceViewer(choice);
			choiceViewer.questionViewer = QuestionViewer.this;
			HorizontalPanel choicePanel = new HorizontalPanel();
			HTML spacer = new HTML();
			spacer.setWidth("30px");
			choicePanel.add(spacer);
			Widget choiceWidget = choiceViewer.getWidget();
			choicePanel.add(choiceWidget);
			choicePanel.setWidth("100%");
			choicePanel.setCellWidth(spacer, "1%");
			choicePanel.setCellWidth(choiceWidget,"99%");
			vpanel.add(choicePanel);
		}
		vpanel.setWidth("100%");
		//vpanel.setBorderWidth(1);
		HorizontalPanel hpanel2 = new HorizontalPanel();
		HTML spacer = new HTML();
		indicator.setVisible(false);
		hpanel2.add(indicator);
		HTML indSpacer = new HTML();
		indSpacer.setWidth("10px");
		hpanel2.add(indSpacer);
		hpanel2.add(itemNumber);
		spacer.setWidth("20px");
		hpanel.add(spacer);
		hpanel.setWidth("100%");
		//hpanel.setBorderWidth(1);
		hpanel2.add(vpanel);
		hpanel2.setCellWidth(itemNumber, "1%");
		hpanel2.setCellWidth(spacer, "1%");
		hpanel2.setCellWidth(spacer, "1%");
		hpanel2.setCellWidth(vpanel, "97%");
		
		return hpanel2;
	}
	public void checkUserAnswer(){
	}

	public void setIndicator(){
		indicator.setVisible(true);
		if(isUserAnswerCorrect()){
			indicator.setUrl("images/correct.png");
		}else{
			indicator.setUrl("images/wrong.png");
		}
	}
	public boolean isUserAnswerCorrect(){
		for(int i = 0; i < question.choiceList.size(); i++){
			Choice c = question.choiceList.get(i);
			if(c.isUserAnswerCorrect()){
				return true;
			}
		}
		return false;
	}
	public void setUserAnswer(Choice answer){
		for(int i = 0; i < question.choiceList.size(); i++){
			Choice choice = question.choiceList.get(i);
			if(choice.equals(answer)){
				choice.setAnswer(true);
			}else{
				choice.setAnswer(false);
			}

		}
	}
}
